package service

import (
	"context"
	__ "video-srv/basic/proto"
	"video-srv/handle/model"
)

type Server struct {
	__.UnimplementedVideoServer
}

func (s *Server) AddVideo(_ context.Context, in *__.AddVideoReq) (*__.AddVideoResp, error) {

	var video model.Video

	video = model.Video{
		Title:       in.Title,
		VideoListId: int32(in.VideoListId),
		VideoUrl:    in.VideoUrl,
		VideoTypeId: int32(in.VideoTypeId),
		Status:      0,
	}

	err := video.AddVideo()
	if err != nil {
		return nil, err
	}

	return &__.AddVideoResp{VideoId: int64(video.Id)}, nil
}

func (s *Server) AddVideoList(_ context.Context, in *__.AddVideoListReq) (*__.AddVideoListResp, error) {

	var videoList model.VideoList

	videoList = model.VideoList{
		Title:      in.Title,
		VideoCount: int8(in.VideoCount),
		Status:     int8(in.Status),
	}

	err := videoList.AddVideoList()
	if err != nil {
		return nil, err
	}

	return &__.AddVideoListResp{VideoListId: int64(videoList.Id)}, nil
}

func (s *Server) CreateUser(_ context.Context, in *__.CreateUserReq) (*__.CreateUserResp, error) {

	var user model.User

	user = model.User{
		Mobile:   in.Mobile,
		Password: in.Password,
	}
	err := user.CreateUser()
	if err != nil {
		return nil, err
	}

	return &__.CreateUserResp{UserId: int64(user.Id)}, nil
}

func (s *Server) GetUser(_ context.Context, in *__.GetUserReq) (*__.GetUserResp, error) {

	var user model.User

	err := user.GetUserByMobile()
	if err != nil {
		return nil, err
	}

	if user.Password != in.Password {
		return nil, nil
	}

	return &__.GetUserResp{Token: in.Token}, nil
}
